<?php
namespace Apps\BoxSns\Model;

use Think\Model;

class SnsFriendsModel extends \BaseModel
{
    function create_friends($data)
    {
        $row = $this->create($data);
        $id = $this->is_friend($row['user_id'], $row['to_user_id']);
        if (!$id) {
            $row['dateline'] = date("Y-m-d H:i:s");
            $id = $this->add($row);
        } else {
            $where = array(
                'user_id' => $row['user_id'],
                'to_user_id' => $row['to_user_id'],
            );
            $r = $this->where($where)->save($row);
        }
        S("user_statistics_" . $row['user_id'], null);
        S("user_statistics_" . $row['to_user_id'], null);
        return $id;
    }

    /*****是否关注他***/
    function is_friend($user_id, $to_user_id)
    {
        $r = $this->where("user_id='$user_id' and to_user_id='$to_user_id'")->find();
        return $r['id'];
    }


    /******用户关系******/
    public function getRel($user_id, $to_user_id)
    {
        if ($user_id == $to_user_id) {
            return array('rel' => 'self', 'text' => '自己');
        }
        $r2 = $this->is_friend($user_id, $to_user_id);
        $r1 = $this->is_friend($to_user_id, $user_id);
        if ($r1 && $r2) {
            return array('rel' => 'each', 'text' => '好友');
        }
        $r = BoxModel("sns_friends_invite")->where("to_user_id='$user_id' and user_id='$to_user_id'")->find();//等待申请通过
        if ($r && dict('need_apply')) {
            return array('rel' => 'apply', 'text' => '等待审核');
        }
        if ($r2) {
            return array('rel' => 'fellow', 'text' => '我关注了他');
        }
        return array('rel' => 'stranger', 'text' => '陌生人');
    }


    /******我的好友列表****/
    function friend_list($where, $limit = 10, $type)
    {
        $count = $this->where($where)->count("*");
        $page = setPager($count, $limit);
        //$limit=$page->firstRow.",".$page->listRows;
        $r = $this->order('id desc')->where($where)->limit($page['limit'])->select();
        //dump($this);exit();
        foreach ($r as $key => $one) {
            if ($type == "fans") {
                $info = BoxModel("SystemMember")->load($one['user_id']);
            }
            if ($type == "follow") {
                $info = BoxModel("SystemMember")->load($one['to_user_id']);
            }
            $one = array_merge($one, $info);
            $list[$key] = $one;
        }
        $data = array(
            'count' => $count,
            'list' => $list,
            'pager' => $page,
        );
        return $data;
    }

    /*******一个好友信息****/
    function load($id)
    {
        $row = $this->find($id);
        $row['user_info'] = BoxModel("SystemMember")->load($one['user_id']);
        $row['to_user_info'] = BoxModel("SystemMember")->load($one['to_user_id']);
        $row['remark'] = $one['remark'];
        return $row;
    }

    /******删除一个好友**/
    function del_friends($user_id, $to_user_id)
    {
        $r = $this->where("user_id='$user_id' and to_user_id='$to_user_id'")->delete();
        S("user_statistics_" . $user_id, null);
        S("user_statistics_" . $to_user_id, null);
        return $r;
    }


}
 